package ch.ffhs.contacts;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.provider.ContactsContract;
import android.util.AttributeSet;

/**
 * class PhonePreference. extends RingtonePreference for selecting an contact instead of an ringtone.
 * 
 * @author mf
 */
public class PhonePreference extends RingtonePreference {

	public PhonePreference(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setUp(context);
	}

	public PhonePreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		setUp(context);
	}

	public PhonePreference(Context context) {
		super(context);
		setUp(context);
	}

	public interface OnPhonePickedListener {
		void onPhonePicked(Uri uri);
	}

	private OnPhonePickedListener mPhonePickedListener;

	public void setOnPhonePickedListener(OnPhonePickedListener listener) {
		if (listener != null) {
			mPhonePickedListener = listener;
		}
	}

	@Override
	protected void onPrepareRingtonePickerIntent(Intent intent) {
		// Override this intent to start ContactsListActivity
		
		intent.setAction(Intent.ACTION_PICK);
		intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
		intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
	}

	@Override
	public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
		if (super.onActivityResult(requestCode, resultCode, data)) {
			if (data != null) {
				Uri uri = data.getData();
				if (callChangeListener(uri != null ? uri.toString() : "")) {

					// get phone-number
					String name = "";
					int hasPhone = 0;
					int idx;
					Cursor cursor = getContext().getContentResolver().query(
							uri, null, null, null, null);
					if (cursor.moveToFirst()) {
						idx = cursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
						name = cursor.getString(idx);

						idx = cursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER);
						hasPhone = Integer.parseInt(cursor.getString(idx));
					}
					if (hasPhone == 1) {
						this.setTitle(name);
						onSaveRingtone(uri);
					} else {
						this.setTitle("-- no phone-number --");
						uri = null;
						onSaveRingtone(uri);
						return false;
					}
				}
			}
			return true;
		} else {
			return false;
		}
	}

	protected void onSaveRingtone(Uri uri) {
		super.onSaveRingtone(uri);
		if (mPhonePickedListener != null) {
			mPhonePickedListener.onPhonePicked(uri);
		}
	}
	
	private void setUp(Context context) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		String uriString = prefs.getString(this.getKey(), "");
		if (!uriString.isEmpty()) {
			Uri uri = Uri.parse(uriString);
			this.setTitle(getTitleText(uri));
		} else {
			this.setTitle("-- leer --");
		}
	}

	private String getTitleText(Uri uri) {
		String name = "";
		Cursor cursor = getContext().getContentResolver().query(uri, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			name = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		} else {
			name = "-- leer --";
		}
		return name;
	}
}
